package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.WmsCargo;
import com.ruoyi.business.domain.WmsInventoryPlan;
import com.ruoyi.business.domain.WmsInventoryRecord;
import com.ruoyi.business.mapper.WmsCargoMapper;
import com.ruoyi.business.mapper.WmsInventoryPlanMapper;
import com.ruoyi.business.mapper.WmsInventoryRecordMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WmsInventoryPlanDetailMapper;
import com.ruoyi.business.domain.WmsInventoryPlanDetail;
import com.ruoyi.business.service.IWmsInventoryPlanDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 盘点计划货物信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-21
 */
@Service
public class WmsInventoryPlanDetailServiceImpl implements IWmsInventoryPlanDetailService
{
    @Autowired
    private WmsInventoryPlanDetailMapper wmsInventoryPlanDetailMapper;

    @Autowired
    private WmsCargoMapper wmsCargoMapper;

    @Autowired
    private WmsInventoryPlanMapper wmsInventoryPlanMapper;

    @Autowired
    private WmsInventoryRecordMapper wmsInventoryRecordMapper;

    /**
     * 查询盘点计划货物信息
     *
     * @param id 盘点计划货物信息主键
     * @return 盘点计划货物信息
     */
    @Override
    public WmsInventoryPlanDetail selectWmsInventoryPlanDetailById(Long id)
    {
        return wmsInventoryPlanDetailMapper.selectWmsInventoryPlanDetailById(id);
    }

    /**
     * 查询盘点计划货物信息列表
     *
     * @param wmsInventoryPlanDetail 盘点计划货物信息
     * @return 盘点计划货物信息
     */
    @Override
    public List<WmsInventoryPlanDetail> selectWmsInventoryPlanDetailList(WmsInventoryPlanDetail wmsInventoryPlanDetail)
    {
        return wmsInventoryPlanDetailMapper.selectWmsInventoryPlanDetailList(wmsInventoryPlanDetail);
    }

    /**
     * 新增盘点计划货物信息
     *
     * @param wmsInventoryPlanDetail 盘点计划货物信息
     * @return 结果
     */
    @Override
    public int insertWmsInventoryPlanDetail(WmsInventoryPlanDetail wmsInventoryPlanDetail)
    {
        wmsInventoryPlanDetail.setCreateTime(DateUtils.getNowDate());
        String cargoSn = wmsInventoryPlanDetail.getCargoSn();
        WmsCargo wmsCargo = wmsCargoMapper.selectWmsCargoBySn(cargoSn);
        if (wmsCargo == null) {
            throw new ServiceException("没有查询到货物信息");
        }
        BeanUtils.copyProperties(wmsCargo, wmsInventoryPlanDetail, "id", "remark", "createBy", "createTime", "updateBy", "updateTime");
        wmsInventoryPlanDetail.setCargoId(wmsCargo.getId());
        int i = wmsInventoryPlanDetailMapper.insertWmsInventoryPlanDetail(wmsInventoryPlanDetail);
        WmsInventoryRecord record = new WmsInventoryRecord();
        record.setInventoryNo(DateUtils.dateTimeNow(DateUtils.YYYYMMDD));
        record.setStatus("wait");
        record.setCreateTime(wmsInventoryPlanDetail.getCreateTime());
        record.setCreateBy(wmsInventoryPlanDetail.getCreateBy());
        WmsInventoryPlan plan = wmsInventoryPlanMapper.selectWmsInventoryPlanByNo(wmsInventoryPlanDetail.getInventoryNo());
        record.setPlanId(plan.getId());
        record.setSupplierSn(plan.getSupplierSn());
        record.setFactoryId(plan.getFactoryId());
        record.setStoreId(plan.getStoreId());
        record.setStoreRegionId(wmsInventoryPlanDetail.getStoreRegionId());
        record.setStoreRegionLocationId(wmsInventoryPlanDetail.getStoreRegionLocationId());
        WmsInventoryRecord oldRecord = wmsInventoryRecordMapper.selectOne(record);
        if (oldRecord == null) {
            wmsInventoryRecordMapper.insertWmsInventoryRecord(record);
        }
        return i;
    }

    /**
     * 修改盘点计划货物信息
     *
     * @param wmsInventoryPlanDetail 盘点计划货物信息
     * @return 结果
     */
    @Override
    public int updateWmsInventoryPlanDetail(WmsInventoryPlanDetail wmsInventoryPlanDetail)
    {
        WmsInventoryPlanDetail old = wmsInventoryPlanDetailMapper.selectWmsInventoryPlanDetailById(wmsInventoryPlanDetail.getId());
        wmsInventoryPlanDetailMapper.deleteWmsInventoryPlanDetailById(wmsInventoryPlanDetail.getId());
        wmsInventoryPlanDetail.setUpdateTime(DateUtils.getNowDate());
        String cargoSn = wmsInventoryPlanDetail.getCargoSn();
        WmsCargo wmsCargo = wmsCargoMapper.selectWmsCargoBySn(cargoSn);
        if (wmsCargo == null) {
            throw new ServiceException("没有查询到货物信息");
        }
        BeanUtils.copyProperties(wmsCargo, wmsInventoryPlanDetail, "id", "remark", "createBy", "createTime", "updateBy", "updateTime");
        wmsInventoryPlanDetail.setCargoId(wmsCargo.getId());
        wmsInventoryPlanDetail.setCreateBy(old.getCreateBy());
        wmsInventoryPlanDetail.setCreateTime(old.getCreateTime());
        int i = wmsInventoryPlanDetailMapper.insertWmsInventoryPlanDetail(wmsInventoryPlanDetail);
        WmsInventoryRecord record = new WmsInventoryRecord();
        record.setInventoryNo(DateUtils.dateTimeNow(DateUtils.YYYYMMDD));
        record.setStatus("wait");
        record.setCreateTime(wmsInventoryPlanDetail.getCreateTime());
        record.setCreateBy(wmsInventoryPlanDetail.getCreateBy());
        WmsInventoryPlan plan = wmsInventoryPlanMapper.selectWmsInventoryPlanByNo(wmsInventoryPlanDetail.getInventoryNo());
        record.setPlanId(plan.getId());
        record.setSupplierSn(plan.getSupplierSn());
        record.setFactoryId(plan.getFactoryId());
        record.setStoreId(plan.getStoreId());
        record.setStoreRegionId(wmsInventoryPlanDetail.getStoreRegionId());
        record.setStoreRegionLocationId(wmsInventoryPlanDetail.getStoreRegionLocationId());
        WmsInventoryRecord oldRecord = wmsInventoryRecordMapper.selectOne(record);
        if (oldRecord == null) {
            wmsInventoryRecordMapper.insertWmsInventoryRecord(record);
        }
        return i;
    }

    /**
     * 批量删除盘点计划货物信息
     *
     * @param ids 需要删除的盘点计划货物信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsInventoryPlanDetailByIds(Long[] ids)
    {
        for (Long id : ids) {
            WmsInventoryPlanDetail planDetail = wmsInventoryPlanDetailMapper.selectWmsInventoryPlanDetailById(id);
            if (planDetail != null) {
                WmsInventoryPlan plan = wmsInventoryPlanMapper.selectWmsInventoryPlanByNo(planDetail.getInventoryNo());
                if (plan != null) {
                    WmsInventoryRecord record = new WmsInventoryRecord();
                    record.setPlanId(plan.getId());
                    record.setStoreRegionId(planDetail.getStoreRegionId());
                    record.setStoreRegionLocationId(planDetail.getStoreRegionLocationId());
                    WmsInventoryRecord oldRecord = wmsInventoryRecordMapper.selectOne(record);
                    if (oldRecord != null) {
                        wmsInventoryRecordMapper.deleteWmsInventoryRecordById(oldRecord.getId());
                    }
                }
            }
        }
        return wmsInventoryPlanDetailMapper.deleteWmsInventoryPlanDetailByIds(ids);
    }

    /**
     * 删除盘点计划货物信息信息
     *
     * @param id 盘点计划货物信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWmsInventoryPlanDetailById(Long id)
    {
        WmsInventoryPlanDetail planDetail = wmsInventoryPlanDetailMapper.selectWmsInventoryPlanDetailById(id);
        if (planDetail != null) {
            WmsInventoryPlan plan = wmsInventoryPlanMapper.selectWmsInventoryPlanByNo(planDetail.getInventoryNo());
            if (plan != null) {
                WmsInventoryRecord record = new WmsInventoryRecord();
                record.setPlanId(plan.getId());
                record.setStoreRegionId(planDetail.getStoreRegionId());
                record.setStoreRegionLocationId(planDetail.getStoreRegionLocationId());
                WmsInventoryRecord oldRecord = wmsInventoryRecordMapper.selectOne(record);
                if (oldRecord != null) {
                    wmsInventoryRecordMapper.deleteWmsInventoryRecordById(oldRecord.getId());
                }
            }
        }
        return wmsInventoryPlanDetailMapper.deleteWmsInventoryPlanDetailById(id);
    }
}
